<%@ include file="shared_header.jsp" %>
<%@ page import="vrp.Customer" %>
<%@ page import="vrp.Product" %>
<%@ page import="vrp.Planning" %>
<%@ page import="vrp.Delivery" %>
<%@ page import="vrp.Route" %>
<%@ page import="vrp.Bundle" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
%>

<script type="text/javascript" src="/scripts/planningroutes.js"></script>

<div id="dialogmap" title="Map Display">
	<div id="map_canvas" style="width: 900px; height: 550px; border: 1px solid black;"></div>
</div>

<div style="padding: 0px 10px;">
	<h2>List of Planning Routes</h2>
	<table style="width: 100%;">
<%
	long keyid = Integer.parseInt(request.getParameter("planning"));
    Key key = KeyFactory.createKey(Planning.class.getSimpleName(), keyid);
    Planning planning = pm.getObjectById(Planning.class, key);
    ArrayList<Route> routes = planning.getRoutes();
    
    Date date = planning.getDate();
    query = "select from " + Delivery.class.getName() + " where date == datepara && status == 0 parameters String datepara";
    List<Delivery> deliveries = (List<Delivery>) pm.newQuery(query).execute(date);

    if (routes.isEmpty()) {
%>
		<tr><td>There is no route for this planning.</td></tr>
<%
    } else {
        for (Route r : routes) {
%>
		<tr>
			<td>
				<div><b>Route:</b></div>
				<div class="route">
<% 
			ArrayList<Key> customerKeys = r.getCustomers();
			ArrayList<Bundle> bundles = r.getBundles();
			String[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
			int customerIdnex = 0;
			for (int i=0; i<customerKeys.size(); i++) {
				Key customerKey = customerKeys.get(i);
				Customer customer = pm.getObjectById(Customer.class, customerKey);
				Bundle bundle = bundles.get(i);
				ArrayList<Key> products = bundle.getProducts();
				ArrayList<Integer> quantities = bundle.getQuantities();
%>
					<div>
						<%= alphabet[customerIdnex] %> - <%= customer.getName() %><br/>
						&nbsp;&nbsp;&nbsp;<%= customer.getAddress() %><br/>
						&nbsp;&nbsp;&nbsp;Singapore <%= customer.getPostalCode() %>
					</div>
					<ul>
<%
				for (int j=0; j<products.size(); j++) {
					Key productKey = products.get(j);
					Product product = pm.getObjectById(Product.class, productKey);
%>
						<li><%= product.getName() %> - <%= quantities.get(j) %> unit(s)</li>
<%					
				}
%>							
					</ul>
					<div class="routePoint" style="display: none;">
						<div class="lat"><%= customer.getLat() %></div>
						<div class="lng"><%= customer.getLng() %></div>
						<div class="title"><%= customer.getAddress() %></div>
						<div class="address"><%= customer.getAddress() + " Singapore " + customer.getPostalCode() %></div>
					</div>
<%
				customerIdnex++;
			}
%>
					<input class="routeButton" type="button"" value="View on Map" />
				</div>
			</td>
		</tr>
<%
        }
    }
%>
			
	</table>
</div>	
<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>